#include <standard.hlsli>
#include <WR2Parameters.hlsli>

VertexToPixel vs_main(VS_INPUT Input)
{
	VertexToPixel Output = (VertexToPixel) 0;
	Output.pos = float4(mul(Input.Position, worldViewProj));
	Output.normal = float3(normalize(mul(Input.Normal, (float3x3) world)));

	Output.kColor = float4(Input.kColor.rgb * 2, Input.kColor.a);
	//Output.kColor = float4(0.5,0.5,0.5,1);
	Output.kBlend = float4(Input.kblend);
	Output.pos2 = Input.Position;

	Output.texCoord1 = float4(Input.Tex.x,
		Input.Tex.y,
		Input.Tex.x * uvTexture2a.x + Input.Tex.y * uvTexture2a.y + uvTexture2a.w,
		Input.Tex.x * uvTexture2b.x + Input.Tex.y * uvTexture2b.y + uvTexture2b.w);

	Output.texCoord2 = float4(Input.Position.x * uvTexture1a.x + Input.Position.y * uvTexture1a.y + Input.Position.z * uvTexture1a.z + uvTexture1a.w,
		Input.Position.x * uvTexture1b.x + Input.Position.y * uvTexture1b.y + Input.Position.z * uvTexture1b.z + uvTexture1b.w,
		Input.Position.x * uvTexture2a.x + Input.Position.y * uvTexture2a.y + Input.Position.z * uvTexture2a.z + uvTexture2a.w,
		Input.Position.x * uvTexture2b.x + Input.Position.y * uvTexture2b.y + Input.Position.z * uvTexture2b.z + uvTexture2b.w);

	Output.texCoord3.xy = float2(Input.Position.x * uvTexture3a.x + Input.Position.y * uvTexture3a.y + Input.Position.z * uvTexture3a.z + uvTexture3a.w,
		Input.Position.x * uvTexture3b.x + Input.Position.y * uvTexture3b.y + Input.Position.z * uvTexture3b.z + uvTexture3b.w);

	float d = distance(Input.Position.xyz, g_CameraPosition);
	float fFogStart = 2000;
	float fFogEnd = 2500;
	Output.foger = saturate((fFogEnd - d) / (fFogEnd - fFogStart));

	return Output;
};

struct PixelToFrame
{
	float4 Color;
};

PixelToFrame ps_main(VertexToPixel PSIn) : SV_TARGET
{
	PixelToFrame Output = (PixelToFrame) 0;
	float3 N = normalize(PSIn.normal);

	float4 Lay1 = xColoredTexture1_Tex.Sample(Tex1, PSIn.texCoord2.xy);
	float4 Lay2 = xColoredTexture2_Tex.Sample(Tex2, PSIn.texCoord2.zw);
	float4 Lay3 = xColoredTexture3_Tex.Sample(Tex3, PSIn.texCoord3.xy);

	float a = smoothstep(0.4375, 0.5625, PSIn.kBlend.r) * Lay3.a;
	float c = smoothstep(0.4375, 0.5625, PSIn.kBlend.b) * Lay1.a * Lay2.a;
	float ShadowCol = smoothstep(0.4375, 0.5625, 1.0 - PSIn.kBlend.a);

	float diffuseColor = max(0.0f, dot(N, -lightDir)) * 1.8f;
	float modulate = 1.0f;
	float3 TexColor = (Lay1.rgb * (1. - c) + Lay2.rgb * c) * (1. - a) + Lay3.rgb * a;
	Output.Color = float4(TexColor.rgb * (SunColor.rgb * diffuseColor * ShadowCol + PSIn.kColor.rgb) * modulate, 1);
	Output.Color = float4(lerp(Output.Color.rgb, FogColor.rgb, PSIn.texCoord3.z), Output.Color.a);
	//if(paintHighLines)
	//{
	//   if(PSIn.original.y % 5.0f < -0.09f && PSIn.original.y % 5.0f  > -0.31f)
	//   {
	//       Output.Color.rgba  =  float4(Output.Color.xyz * 1.5f,0.50f);
	//   }
	//}

	return Output;
}

technique Material00
{
	pass Material00
	{
		VertexShader = compile vs_1_1 vs_main();
		PixelShader = compile ps_1_0 ps_main();
	}
}